import os

import yaml


def read_yaml(fileName):
    """
    读取 yaml 文件
    :param fileName: 文件名称
    :return: dict/list
    """
    # yaml文件读取路径，默认存在data目录下
    filePath = get_path(f"data/{fileName}")
    with open(filePath, 'r', encoding='utf-8') as f:
        data = yaml.safe_load(f)
    return data


def write_yaml(fileName, data):
    """
    写入 yaml 文件
    :param fileName: 文件名称
    :param data: 写入数据
    """
    # yaml文件读取路径，默认存在data目录下
    filePath = get_path(f"data/{fileName}")
    with open(filePath, 'w', encoding='utf-8') as f:
        yaml.safe_dump(data, f)


def get_path(path_name):
    """
    获取绝对路径
    :param path_name: 目录名称
    :return: 目录绝对路径
    """
    # 获取当前工具文件所在的路径
    root_path = os.path.dirname(os.path.abspath(__file__))
    # 拼接当前要输出日志的路径
    dir_path = os.sep.join([root_path, '..', path_name])
    return dir_path
